//
//  WaterFallLayout.m
//  Rippton2.0
//
//  Created by 胡旭辉 on 2019/8/19.
//  Copyright © 2019 胡旭辉. All rights reserved.
//

#import "WaterFallLayout.h"

@interface WaterFallLayout ()
/** 这个字典用来存储每一列最大的Y值(每一列的高度) */
@property (nonatomic, strong) NSMutableDictionary *maxYDict;
/** 存放所有的布局属性 */
@property(nonatomic,strong)NSMutableArray *attributeArray;

@end

@implementation WaterFallLayout

- (NSMutableDictionary *)maxYDict{
    if (!_maxYDict) {
        _maxYDict = [NSMutableDictionary dictionary];
    }
    return _maxYDict;
}
- (NSMutableArray *)attributeArray{
    if (!_attributeArray) {
        _attributeArray = [NSMutableArray array];
    }
    return _attributeArray;
}

- (instancetype)init{
    if (self = [super init]) {
        self.columnsCount = 2;
        self.minimumInteritemSpacing = 0;
        self.minimumLineSpacing = 0;
        self.sectionInset = UIEdgeInsetsZero;
    }
    return self;
}
- (void)prepareLayout{
    [super prepareLayout];
    
    // 清空最大的Y值
    for (int i = 0; i < self.columnsCount; i++) {
        NSString *column = [NSString stringWithFormat:@"%i",i];
        self.maxYDict[column] = @(self.sectionInset.top);
    }
    
    [self.attributeArray removeAllObjects];
    
    if (self.isNeedHeader) {
        //头部视图
        UICollectionViewLayoutAttributes * layoutHeader = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathWithIndex:0]];
        layoutHeader.frame =CGRectMake(0,0, UIScreen.mainScreen.bounds.size.width,[self.delegate WaterHeaderHeight]);
        [self.attributeArray addObject:layoutHeader];
    }

    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int index = 0; index < count; index++) {
        UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
        [self.attributeArray addObject:attr];
    }
}
- (CGSize)collectionViewContentSize{
    __block NSString *maxColumn = @"0";
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSNumber * _Nonnull obj, BOOL * _Nonnull stop) {
        if ([obj floatValue] > [self.maxYDict[maxColumn] floatValue]) {
             maxColumn = key;
        }
    }];
    return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 计算宽度
    CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.minimumInteritemSpacing) / self.columnsCount;
    CGFloat height = [self.delegate flowLayout:self heightForIndexPath:indexPath];
    __block NSString *minColumn = @"0";
    // 遍历字典找出最短的那一列
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
        if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {
            minColumn = column;
        }
    }];
    
    // 2.1计算位置
    CGFloat x = self.sectionInset.left + (self.minimumInteritemSpacing + width) * [minColumn intValue];
    CGFloat headerHeight = .0f;
    if (self.isNeedHeader) {
        headerHeight = [self.delegate WaterHeaderHeight];
    }
    if (indexPath.item >= self.columnsCount) {
        headerHeight = .0f;
    }
    
    CGFloat y = [self.maxYDict[minColumn] floatValue] + self.minimumLineSpacing + headerHeight;
    
    self.maxYDict[minColumn] = @(y + height);
    // 3.创建属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attrs.frame = CGRectMake(x, y, width, height);
    return attrs;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}
//返回rect范围内item的attributes
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attributeArray;
}


@end
